package com.francisco.ets.controleur;

import java.awt.Graphics;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.UnknownHostException;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import com.francisco.ets.modele.Forme;
import com.francisco.ets.util.CommBase;
import com.francisco.ets.util.GestionaireFormes;
import com.francisco.ets.util.Labo1Const;
import com.francisco.ets.util.LangueConfig;
import com.francisco.ets.util.Labo1Const.ProprietesChangees;
import com.francisco.ets.visuel.FenetreFormes;
import com.francisco.ets.visuel.FenetrePrincipale;

public class ControleurFrontal implements PropertyChangeListener
{
	private static final long serialVersionUID = -1210804336046370508L;
	public ControleurFrontal(FenetrePrincipale fenForm)
	{
		fenetrePrincipaleRef=fenForm;
	}
	public ControleurFrontal(){}
	private static final String MESSAGE_SERVEUR="app.frame.dialog.serverDialog";
	private static final String MESSAGE_ERREUR_NOM="";
	private PropertyChangeListener listener = null;
	private CommBase commBaseRef;	
	private FenetrePrincipale fenetrePrincipaleRef;
	private Socket sockClient;
	private String[] ParamConnection;
	private InetSocketAddress sockAdress;
	public void doAction(Labo1Const.Actions action)
	{
		switch(action)
		{
			case AFFICHER_FORMES:
			{
				if(fenetrePrincipaleRef!=null)
				{
					fenetrePrincipaleRef.repaint();
				}
				break;
			}
			case AFFICHER_DIALOGUE_CONNECTION:
			{
				
				String reponse=JOptionPane.showInputDialog(fenetrePrincipaleRef,LangueConfig.getResource(MESSAGE_SERVEUR), "localhost:10000");
				if (reponse==null)
				{
					doAction(Labo1Const.Actions.QUITTER_APPLICATION);
				}
				else
				{
					ParamConnection=reponse.split(":");
					doAction(Labo1Const.Actions.VERFICATION_CONNECTION_SERVEUR);
					
				}
				break;
			}
			case VERFICATION_CONNECTION_SERVEUR:
			{
				try 
				{
					int port;
					try
					{
						port=Integer.parseInt(ParamConnection[1]);	
					}
					catch(NumberFormatException e)
					{
						JOptionPane.showMessageDialog(null,"DNS invalide");
						break;
					}
					sockAdress=new InetSocketAddress(ParamConnection[0],port);
					if(sockAdress.isUnresolved())
					{
						JOptionPane.showMessageDialog(null,"DNS invalide");
						doAction(Labo1Const.Actions.AFFICHER_DIALOGUE_CONNECTION);
						break;
					}
					sockClient = new Socket(sockAdress.getHostName(),sockAdress.getPort());
					
				} 
				catch (UnknownHostException e) 
				{
					e.printStackTrace();
				} 
				catch (IOException e) 
				{	
					e.printStackTrace();
				}
				if(sockClient!=null)
				{
					if(!fenetrePrincipaleRef.isVisible())
					{
						fenetrePrincipaleRef.setVisible(true);
					}
					commBaseRef.setConnectionClient(sockClient);
				}
				else
				{
					JOptionPane.showMessageDialog(fenetrePrincipaleRef,"Le serveur:"+sockAdress.getHostName()+" ne repond pas sur le port:"+sockAdress.getPort());
					doAction(Labo1Const.Actions.AFFICHER_DIALOGUE_CONNECTION);
				}
				break;
			}
			case ARRETER_THREAD:
			{
				commBaseRef.stop();
				JOptionPane.showMessageDialog(null,"Le serveur a arrete de fonctioner");
				break;
			}
			case QUITTER_APPLICATION:
			{
				//verification serveur avant
				System.exit(0);
			}
		}
	}
	public void setCommBaseRef(CommBase commBaseRef) 
	{
		this.commBaseRef = commBaseRef;
	}
	
	public void setListener(PropertyChangeListener listener) 
	{
		this.listener = listener;
	}
	@Override
	public void propertyChange(PropertyChangeEvent evt) 
	{
		if(evt.getPropertyName().equals(ProprietesChangees.SERVEUR_TOMBE.toString()))
		{
			this.doAction(Labo1Const.Actions.ARRETER_THREAD);
		}
		if(evt.getPropertyName().equals(ProprietesChangees.FORME_AJOUTEE.toString()))
		{
			GestionaireFormes.getInstance().ajouterFigure((String)evt.getNewValue());
			this.doAction(Labo1Const.Actions.AFFICHER_FORMES);
		}
		
	}
	
}
